package lib::ResultTreatment;

# This file is part of noplagia.
#
# noplagia is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# noplagia is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# noplagia; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA  02111-1307 USA

use strict;
use Data::Dumper;
use lib::TextTreatment;
use lib::Compare;
use lib::Context;
require Exporter;
our ($VERSION,@ISA,@EXPORT,$context);

$VERSION = 0.01;

=head1 NAME

    lib::ResultTreatment - Bibliotheque de manipulation des resultats

=head1 SYNOPSIS

=head1 DESCRIPTION

    Ce module manipule la liste des resultats fournis par Req.pm.

=head1 FUNCTIONS

=over 2

=cut

@ISA    = qw(Exporter);
@EXPORT = qw(
    &result_treatment
);

=item result_traitement

    result_traitement(
        lib::Req::request(
            {
                text   => $_,
                engine => "Yahoo",
                count  => "15",
                lang   => $text_langage,
            }
        )
    );

=cut

sub result_treatment {
    my $refListe = shift;
    my @liste = @$refListe;
    my @banis = split(/[\Q ,;\E]+/, get_pref('disable_domaine_site'));
    my @returnL;
    my $totalnb = 0;
    DOM: while(@liste){
        my %h;
        my $hliste = shift (@liste);
        my $domaine = (split (/\//,$hliste->{url}))[2];
        foreach (@banis){
            next DOM if ($_ eq $domaine) ;
        }
        $h{nb} = 1;
        $h{domaine} = $domaine;
        push @{$h{urls}} , $hliste;
        my @newListe=();
        my $url_id = 0;
        foreach my $hl (@liste){
            if ( $domaine eq (split (/\//,$hl->{url}))[2]){
                $h{nb}++;
                my $nb_eq = 0;
                foreach my $ash (@{$h{urls}}){
                    if($hl->{url} eq $ash->{url}){
                        $ash->{url_id} = $url_id;
                        $url_id++;
                        $nb_eq++;
                        $ash->{nb}++;
                        push @{$ash->{text_offset}} , @{$hl->{text_offset}};
                    }
                }
                if($nb_eq == 0){
                    push @{$h{urls}} , $hl;
                }
            }else{
                push @newListe , $hl;
            }
        }
        my $total_match = 0;
        foreach (@{$hliste->{text_offset}}){
            $_->{match} = sprintf("%0.2f", _compare(set_noPunctuation($_->{text}),set_noPunctuation($_->{summary}))*100);
            $total_match += $_->{match} if $_->{match} > 0;
        }
        if (scalar(@{$hliste->{text_offset}} > 0)) {
            $hliste->{match} = sprintf("%0.2f", $total_match / scalar(@{$hliste->{text_offset}}));
        }else{
            $hliste->{match} = $total_match;
        }
        @liste = @newListe;
        $totalnb += $h{nb};
        push @returnL , \%h;
    }
    my @return = sort {$b->{nb} <=> $a->{nb}}  @returnL;
    my $moyenne = $totalnb / scalar(@return) if scalar(@return) > 0;
    unshift @return , $moyenne;
    return (\@return);
}

END { }
1;
__END__

=back

=head1 AUTEUR

    noplagia dev team

    http://noplagia.org

=cut
